package cn.echoedu.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class ImageUtil {
	private static final String T = "ImageUtil------->>";
	public static String imageTobase64Str(String imageurl){
		return encodeForUrl(new BASE64Encoder().encode(imageToBytes(imageurl)));

	}
	/**
	 * ͨ��http:url���˿ں�/+����ֵ   ���Է��ʵ�ͼƬ
	 * @param imagebase64
	 * @param imagename
	 * @return ����ͼƬ���·�������ļ��к��ļ���
	 */
	public static String base64strToImage(String imagebase64,String filegroup,
			String imagename){
		final String M="base64strToImage********>>";
		L.p(T+M+imagebase64);
		if(isNull(imagebase64)){
			return null;
		}
		try{
			byte [] b = decodeForUrlToBytes(imagebase64);
	 		 String filetype = ".png";
			String imageurl = filegroup+"/"+imagename+filetype;
			L.p(T+M+"imageurl"+imageurl);
			createFileGroupWhenNotExists(filegroup);
			OutputStream out = new FileOutputStream(getAbsoluteFilePath(imageurl));
			out.write(b);
			out.flush();
			out.close();
			return imageurl;
		}catch(Exception e){
			return null;
		}
	}
	private static boolean isNull(String imagebase64) {
		// TODO Auto-generated method stub
		return imagebase64==null?true:false;
	}
	private static boolean createFileGroupWhenNotExists(String filegroup) throws IOException{
		File file= new File(PathUtil.getProjectParentPath()
				+File.separator+filegroup);
		String path = PathUtil.getProjectParentPath()
				+File.separator+filegroup;
		System.out.println("path::::::"+path);
		if(!file.exists()){
			return file.mkdirs();
		}
		return true;

	}
	public static boolean deleteFileByGroupAndName(String imageurl){
		File file = new File(PathUtil.getProjectParentPath()+File.separator+
				imageurl);
		if(file.exists()){
			return file.delete();
		}
		return false;
	}
	private static String getAbsoluteFilePath(String imageurl){
		String path=PathUtil.getProjectParentPath()
				+File.separator+imageurl;
		L.p(T+"path"+path);
		return path;
	}
	private static byte[] base64strToBytes(String imagebase64){
		byte[] b;
		try {
			b = new BASE64Decoder().decodeBuffer(imagebase64);
			for(int i=0;i<b.length;++i){
				if(b[i]<0){
					b[i]+=256;//�����쳣���
				}
			}
			return b;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
	private static byte[] imageToBytes(String imageurl){
		InputStream in = null;
		byte[] imagebyte=null;
		try{
			in = new FileInputStream(imageurl);
			imagebyte = new byte[in.available()];
			in.read(imagebyte);
			in.close();
		}catch(IOException e){
			e.printStackTrace();
		}
		return imagebyte;
	}
	 
	/**
	 * �� s ���� BASE64 ����,���url�ı���
	 * @param s
	 * @return
	 */
	public static String encodeForUrl(String base64str){
		if (base64str == null)
			return null;
		String standerBase64 = base64str;
		String encodeForUrl = standerBase64;
		//ת�����url��base64����
		encodeForUrl = encodeForUrl.replace("=", "");
		encodeForUrl = encodeForUrl.replace("+", "*");
		encodeForUrl = encodeForUrl.replace("/", "-");
		//ȥ����
		encodeForUrl = encodeForUrl.replace("\n", "");
		encodeForUrl = encodeForUrl.replace("\r", "");
		//ת��*��Ϊ -x-
		//��ֹ��Υ��Э����ַ�
		encodeForUrl = encodeSpecialLetter1(encodeForUrl);
		return encodeForUrl;
	}
	
	 
	/**�� BASE64 ������ַ� s ���н���
	 *
	 * @param s
	 * @return
	 */
	public static byte[] decodeForUrlToBytes(String s) {
		if (s == null)
			return null;
		s = decodeSpecialLetter1(s);
		s = s.replace("*", "+");
		s = s.replace("-", "/");
		s += "=";
		sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
		try {
			byte[] b = decoder.decodeBuffer(s );
			return b;
		} catch (Exception e) {
			return null;
		}
	}
	/**
	 * ת��*��Ϊ -x-��
Ϊ�˷�ֹ��Υ��Э����ַ�-x ת��Ϊ-xx
	 * @param str
	 * @return
	 */
	private static String encodeSpecialLetter1(String str){
		str = str.replace("-x", "-xx");
		str = str.replace("*", "-x-");
		return str;
	}
	/**
	 * ת�� -x-��Ϊ*��-xxת��Ϊ-x
	 * @param str
	 * @return
	 */
	private static String decodeSpecialLetter1(String str){
		str = str.replace("-x-", "*");
		str = str.replace("-xx", "-x");
		return str;
	}
	 
}
